package com.luojie.service.impl;


import com.luojie.dto.BallDTO;
import com.luojie.mapper.BallMapper;
import com.luojie.mapper.MatchesMapper;
import com.luojie.po.Ball;
import com.luojie.po.Matches;
import com.luojie.service.BallService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BallServiceImpl  implements BallService {
    @Autowired
    private BallMapper ballMapper;
    @Autowired
    private MatchesMapper matchesMapper;

    @Override
    public void addball(BallDTO ballDTO) {
        Ball ball=new Ball();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime  kickOffTime = LocalDateTime.parse(ballDTO.getKickOffTime(), formatter);
        ball.setMatchesId(ballDTO.getMatchesId());
        ball.setBallGamesName(ballDTO.getBallGamesName());
        ball.setCompetitionFormat(ballDTO.getCompetitionFormat());
        ball.setBallGames(ballDTO.getBallGames());
        ball.setKickOffTime(kickOffTime);
        ball.setRestriction(ballDTO.getRestriction());
        ball.setTotalNumberParticipants(ballDTO.getTotalNumberParticipants());
        ball.setNumberParticipants(0);
        ball.setRegistrationFee(ballDTO.getRegistrationFee());
        ball.setCreatedTime(LocalDateTime.now());
        ball.setUpdatedTime(LocalDateTime.now());
        List<Ball> ballList =ballMapper.selectByMachesId(ballDTO.getMatchesId());
        List<String> name = ballList.stream().map(Ball::getBallGamesName).collect(Collectors.toList());
        List<Integer> id = ballList.stream().map(Ball::getId).collect(Collectors.toList());
        Matches matches =new Matches();
        if (id.size()==1){
            for (Integer integer : id) {
              matches=   matchesMapper.selectById(integer);
            }
        }
        for (String s : name) {
            if (s.equals(ball.getBallGamesName())){
                throw new RuntimeException("该比赛项目已存在");
            }
        }
        if (matches.getRegistrationTime().isBefore(LocalDateTime.now()) && matches.getRegistrationEndTime().isAfter(LocalDateTime.now())) {
            matches.setStatus(1);
            ballMapper.addball(ball);
        } else {
            matches.setStatus(0);
            ballMapper.addball(ball);
        }


    }

    @Override
    public void updateBall(BallDTO ballDTO) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime  kickOffTime = LocalDateTime.parse(ballDTO.getKickOffTime(), formatter);
        Ball ball=new Ball();
        ball.setMatchesId(ballDTO.getMatchesId());
        ball.setBallGamesName(ballDTO.getBallGamesName());
        ball.setCompetitionFormat(ballDTO.getCompetitionFormat());
        ball.setBallGames(ballDTO.getBallGames());
        ball.setKickOffTime(kickOffTime);
        ball.setRestriction(ballDTO.getRestriction());
        ball.setTotalNumberParticipants(ballDTO.getTotalNumberParticipants());
        ball.setNumberParticipants(0);
        ball.setRegistrationFee(ballDTO.getRegistrationFee());
        ball.setCreatedTime(LocalDateTime.now());
        ball.setUpdatedTime(LocalDateTime.now());
        ballMapper.updateBall(ball);
    }
}
